package com.recommend.recommendkeyword.service.impl;

import com.recommend.recommendkeyword.entity.*;
import com.recommend.recommendkeyword.mapper.*;
import com.recommend.recommendkeyword.service.KeywordService;
import com.recommend.recommendkeyword.service.SearchService;
import com.recommend.recommendkeyword.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    SearchService searchService;
    @Autowired
    RecommendKeywordMapper recommendKeywordMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    LikeHistoryMapper likeHistoryMapper;
    @Autowired
    KeyWordsMapper keyWordsMapper;
    @Autowired
    CommentMapper commentMapper;

    //调用python接口，产生一个新的权重
    public double getNewWeight(String seedKeyword, String recommendKeyword){
        double newWeight = 0.5;;
        return newWeight;
    }

    @Override
    public int isLike(String recommendName, String seedKeyword, int isLike, int userId) {
        RecommendKeyword recommendKeyword = recommendKeywordMapper.getWordBySeedAndRecommend(recommendName, seedKeyword);
        Keywords keywords = keyWordsMapper.getKeywordBySeed(seedKeyword);
        User user = userMapper.selectUserById(userId);
        int vip = user.getVip();//0表示非VIP，1表示VIP
        //增加点赞/点踩数：非vip加一，vip加三
        int likeNum = (vip == 1) ? 3 : 1;
        int rows1 = recommendKeywordMapper.isLike(recommendName, seedKeyword, isLike, likeNum);

        //修改点赞记录表
        Timestamp now = new Timestamp(System.currentTimeMillis());
        int type = (isLike == 0) ? 1 : 2;
        likeHistoryMapper.insertHistory(userId, recommendKeyword.getRecommendId(), type, now);

        //然后如果是竞争关键词的话，直接返回
        if(recommendKeyword.getType() == 1)
            return rows1;

        //如果是中介关键词的话，则调用py接口，参数seedKeyword, recommendName，返回这个词的新权重
        double newWeight = getNewWeight(seedKeyword, recommendName);
        System.out.println("newWeight:" + newWeight);
        System.out.println("原来的Intermediary:" + keywords.getIntermediary());
        System.out.println("原来的weightList:" + keywords.getWeight());
        List<Integer> intermediaryList = SearchServiceImpl.splitAndConvert(keywords.getIntermediary(), Integer.class);
        List<Double> weightList = SearchServiceImpl.splitAndConvert(keywords.getWeight(), Double.class);
        System.out.println("处理后的IntermediaryList:" + intermediaryList);
        System.out.println("处理后的weightList:" + weightList);
        int index = intermediaryList.indexOf(recommendKeyword.getRecommendId());
        System.out.println("index:" + index);
        weightList.set(index, newWeight);
        System.out.println("新权重之后的weightList:" + weightList);
        //组合成新的权重字符串，并插入
        StringBuilder weightString = new StringBuilder();
        for(double weight:weightList)
            weightString.append(weight).append("/");
        weightString = new StringBuilder(weightString.substring(0, weightString.length() - 1));
        System.out.println("weightString:" + weightString);
        keyWordsMapper.setWeight(keywords.getKeywordId(), weightString.toString());

        return rows1;
    }

    @Override
    public int comment(String recommendName, String seedKeyword, String comment, int userId){
        //先给推荐关键词的评论数加1
        RecommendKeyword recommendKeyword = recommendKeywordMapper.getWordBySeedAndRecommend(recommendName, seedKeyword);
        int rows1 = recommendKeywordMapper.addCommentNum(recommendKeyword.getRecommendId());

        //修改评论表
        Timestamp now = new Timestamp(System.currentTimeMillis());
        int rows2 = commentMapper.insertComment(recommendKeyword.getRecommendId(), userId, now, comment);

        return rows1;
    }

    @Override
    public List<CommentVO> getComment(int recommendId){
        List<Comment> commentList = commentMapper.getCommentByRecommend(recommendId);
        List<CommentVO> commentVOList = new ArrayList<>();
        for(Comment comment: commentList){
            String username = userMapper.getNameById(comment.getUserId());
            CommentVO commentVO = new CommentVO(username, comment.getCommentContent(), comment.getCommentTime());
            commentVOList.add(commentVO);
        }
        return commentVOList;
    }
}
